#include <QtGui/QMouseEvent>
#include "glwidget.h"
#include <QDebug>
#include <Windows.h>

GLWidget::GLWidget(QWidget *parent,int _w,int _h,int _c,int predef) : QGLWidget(parent) {
    setMouseTracking(true);
    colors[0][0] =  0;      colors[0][1] =  0;      colors[0][2] = 0;
    colors[1][0] =  0.9;    colors[1][1] =  0.0;    colors[1][2] =  0.0;
    colors[2][0] =0.0;    colors[2][1] =  0.8;    colors[2][2] =  0.0;
    colors[3][0] =  0;      colors[3][1] =  0;      colors[3][2] =  1.0;
    colors[4][0] =  1.0;    colors[4][1] =  1.0;    colors[4][2] =  0;
    colors[5][0] =  1.0;    colors[5][1] =  0.0;    colors[5][2] =  1.0;
    colors[6][0] =  0;      colors[6][1] =  1.0;    colors[6][2] =  1.0;
    colors[7][0] =  1.0;    colors[7][1] =  1.0;    colors[7][2] =  1.0;
    colors[8][0] =  0.5;    colors[8][1] =  1.0;    colors[8][2] =  0.5;

    if(predef<0)
    {
    int w = _w ;
    int h = _h;
    c = _c;

    s = new sameGame(w,h,c);



    }
    else
    {
        _w = 15;
        _h  = 15;
        c = 5;
        s = new sameGame(predef);
    }
    step = 0;
   // timer  = new QTimer(this);
    // connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
    offset = 50;
    mousePress = false;
    // timer->start(100);
    paintColor = 0;

}
GLWidget::GLWidget(QWidget *parent,sameGame *samegame,SGSolution solution_) : QGLWidget(parent) {
    setMouseTracking(true);
    colors[0][0] =  0;      colors[0][1] =  0;      colors[0][2] = 0;
    colors[1][0] =  0.9;    colors[1][1] =  0.0;    colors[1][2] =  0.0;
    colors[2][0] =0.0;    colors[2][1] =  0.8;    colors[2][2] =  0.0;
    colors[3][0] =  0;      colors[3][1] =  0;      colors[3][2] =  1.0;
    colors[4][0] =  1.0;    colors[4][1] =  1.0;    colors[4][2] =  0;
    colors[5][0] =  1.0;    colors[5][1] =  0.0;    colors[5][2] =  1.0;
    colors[6][0] =  0;      colors[6][1] =  1.0;    colors[6][2] =  1.0;
    colors[7][0] =  1.0;    colors[7][1] =  1.0;    colors[7][2] =  1.0;
    colors[8][0] =  0.5;    colors[8][1] =  1.0;    colors[8][2] =  0.5;


        s = samegame;
        c = s->colors();


    step = 0;
    timer  = new QTimer(this);
    // connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
    offset = 50;
    mousePress = false;
    solution = solution_;
    // timer->start(100);
    paintColor = 0;

}

void GLWidget::initializeGL() {
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_COLOR_MATERIAL);
    glEnable(GL_BLEND);
    glEnable(GL_POLYGON_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0, 0, 0, 0);
}

void GLWidget::resizeGL(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //gluOrtho2D(0, w, 0, h);// set origin to bottom left corner
    glOrtho(0,w,0,h,0,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void GLWidget::paintGL() {

    long long start,stop;
    start = GetTickCount();
    for(int i=0;i<1;i++)
    {
        //scena::clear();
        //qDebug() << "clear done";
        if(rect().height()>50)
        {
            int w = s->w();
            int h = s->h();

            int wid = rect().width();
            int hei = rect().height()-offset;


            glClear(GL_COLOR_BUFFER_BIT);
            int x,y;
            x=w-1;

            for(int i=0;i<w;++i,--x)
            {
                y=h-1;
                for(int j=0;j<h;++j,--y)
                {
                   // qDebug()<< s->getColor(i,j);
                    int color = s->getColor(i,j);
                    float light = 0.8;
                    glColor3f(colors[color][0]*light,colors[color][1]*light,colors[color][2]*light);
                    // glColor3f(1.0,1.0,1.0);
                    glRectf(i*wid/w+1 , y*hei/h+1 , i*wid/w + wid/w-2,y*hei/h + hei/h-2);
                }
            }



        }
        //  glEnd();
    }
    stop = GetTickCount();
    glColor3f(1.0,1.0,1.0);
    renderText(0,height()-10.0,0.0,QString::number((stop-start))+QString("ms"));
    renderText(0,height()-20.0,0.0,QString::number(s->groupCount())+QString("groups"));
    renderText(0,height()-30.0,0.0,QString::number(s->getResult())+QString("blocks"));
    renderText(0,height()-40.0,0.0,QString::number(s->getScore())+QString("points"));


    for(int i=0;i<=c;++i)
    {
        glColor3f(colors[i][0],colors[i][1],colors[i][2]);
        glRectf(width()-30*i,height() -5,width()-30*i-30,height() -35);
    }
    //qDebug() << "rects done";



}

void GLWidget::mousePressEvent(QMouseEvent *event) {

mousePress  = true;
if(solution.steps.size()>0)
{
    if(step < solution.steps.size())
    {
   //     qDebug()<< "klik" << solv->bestBee->steps.at(step).x << solv->bestBee->steps.at(step).y;
 //       tick();
    }
    else
    {
        //delete solution;
        solution.steps.clear();
        //solution = SGSolution();
        step = 0;
    }
    return;
}
}
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    mousePress = false;
    if(solution.steps.size()>0)
    {
        if(step < solution.steps.size())
        {
            qDebug()<< "klik" << solution.steps.at(step).x << solution.steps.at(step).y;
            tick();
        }
        else
        {
            //delete solution;
            solution.steps.clear();
            step = 0;
        }
        return;
    }
    else
    {
        qDebug()<< "klik" ;
        if(event->pos().y()<35)
        {
            paintColor =  ((width()-event->pos().x())/30);
            qDebug()<< "paintcolor"<<  paintColor;
            if(paintColor>c)
            {
                //solv= new Solver(*s);
            }
            return;
        }
        int x = (int)(((float)event->pos().x())/rect().width()*s->w());
        int y = (int)(((float)event->pos().y()-offset)/(rect().height()-offset)*s->h());
        if(0==paintColor)
        {
            qDebug()<< "0paintColor"<< paintColor;
            if(x<0 || x>=s->w() || y<0 || y>=s->h()) return;
            qDebug() << "MX:" << x;
            qDebug() << "MY:" << y;
            click(x,y);
        }
        else
        {
            s->setColor(x,y,paintColor);
           // updateGL();
        }

    }
      update();
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
    // printf("%d, %d\n", event->x(), event->y());
    if(!mousePress) return;
    int x = (int)(((float)event->pos().x())/rect().width()*s->w());
    int y = (int)(((float)event->pos().y()-offset)/(rect().height()-offset)*s->h());
    if(0<paintColor&& paintColor<=c)
    {
        if(x<0 || x>=s->w() || y<0 || y>=s->h()) return;
        s->setColor(x,y,paintColor);
        updateGL();
    }
}

void GLWidget::keyPressEvent(QKeyEvent* event) {
    switch(event->key()) {
    case Qt::Key_Escape:
        close();
        break;
    default:
        event->ignore();
        break;
    }
}
